package com.yunhe.ssm.struts.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yunhe.ssm.mybatis.dao.StudentDao;
import com.yunhe.ssm.mybatis.model.Page;
import com.yunhe.ssm.mybatis.model.StudentModel;
import com.yunhe.ssm.mybatis.service.IStudentService;

public class StudentAction extends ActionSupport implements ModelDriven<Object>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private IStudentService studentservice;
	@Autowired
	private StudentDao studentdao;
	private StudentModel studentmodel;

	private Page page;
	private int curPage;
	private int pageSize;
	private File photo;
	private String photoFileName;
	
	
	
	
	
	public File getPhoto() {
		return photo;
	}
	public void setPhoto(File photo) {
		this.photo = photo;
	}
	public String getPhotoFileName() {
		return photoFileName;
	}
	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}

	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public StudentModel getStudentmodel() {
		return studentmodel;
	}
	public void setStudentmodel(StudentModel studentmodel) {
		this.studentmodel = studentmodel;
	}
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
	public void login() throws IOException{
		
		PrintWriter out=response.getWriter();
		
			
			StudentModel student=studentservice.loginStu(studentmodel);
			if(student!=null){
				out.print(JSON.toJSON(student));
			}
	}
	
	public String getStudentById() throws IOException{
		 response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		request.setAttribute("student",studentservice.getStudentById(studentmodel) );
		request.setAttribute("soccer",studentservice.getSoccerById(studentmodel) );
		request.setAttribute("teachers",studentservice.getTeacherByStu(studentservice.getStudentById(studentmodel)));
		return SUCCESS;
		
		
	}
	@Override
	public Object getModel() {
		
		if(studentmodel==null){
			studentmodel=new StudentModel();
		}
		return studentmodel;
	}
	
	public String getStudentPage(){
		Map<String,Object> cond=new HashMap<String,Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", curPage);
		page=studentservice.getStudentPage(cond);
		
		
		return SUCCESS;
	}
	
	public void upstudent() throws IOException{
		System.out.println(studentmodel.getId());
		System.out.println(studentmodel.getName());
		studentservice.studentUp(studentmodel);
			}
	public void stupwd() throws IOException{
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		studentservice.stuUpwd(studentmodel);
		out.print("{\"msg\":\"密码修改成功！\"}");
	}
	
	public void findPwdById() throws IOException{
		 response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		if(studentservice.finPwdById(studentmodel)==null){
			out.print("{\"msg\":\"旧密码输入错误，请重新输入！\"}");
		}else{
			out.print("{\"msg\":\"\"}");
		}
		
	
	}

}
